package rock.negagame.client.sprites;

import rock.negagame.client.sprites.control.GameController;
import rock.negagame.client.sprites.control.Pantalla;
import rock.negagame.client.sprites.enemigos.Movimiento;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Image;

public class Sprite extends Composite{

	int posX;
	int posY;
	Image img;

	public boolean explotar=false;
	int contador = 0;
	
	int vida;
	
	public Sprite(Image img){
		this.img = img;
		posX = 0;
		posY = 0;
		
		
		this.initWidget(this.img);
		this.setStyleName("sprite");
	}
	public int getWidth(){
		return img.getOffsetWidth();
	}
	public int getHeight(){
		return img.getOffsetHeight();
	}
	public void mueve(int direccion, int dist){
		switch(direccion){
		case Movimiento.DCH: moveRigth(dist);
			break;
		case Movimiento.IZQ: moveLeft(dist);
		break;
		case Movimiento.SUP: moveUp(dist);
		break;
		case Movimiento.INF: moveDown(dist);
		break;
		default:
			break;
		}
	}
	public void explota(){
		explotar=true;
		if(contador>19){
			GameController.getGame().elimina(this);
		}else{
			//this.img = Explosion.getInastance().getImagen(contador);
			int w = this.img.getOffsetWidth();
			int h = this.img.getOffsetHeight();
			
			this.img.setUrl(Explosion.getInastance().getImagen(contador).getUrl());
			int w1 = this.img.getOffsetWidth();
			int h1 = this.img.getOffsetHeight();
			
			int dx = (w1-w)/2;
			int dy = (h1-h)/2;
			
			this.setPosition(this.getPosX()-dx, this.getPosY()-dy);
			contador++;
		}
	}
	
	public void moveLeft(int dist){
		if(posX<10&&this instanceof Jugador){
			
		}else{
			posX = posX-dist;
			setPosition();
		}		
	}
	public void moveRigth(int dist){
		if(posX>(Pantalla.w-10-this.getOffsetWidth())&&this instanceof Jugador){
			
		}else{
			posX = posX+dist;
			//DOM.setStyleAttribute(this.getElement(), "top", ""+posY);
			//DOM.setStyleAttribute(this.getElement(), "left", ""+posX);
			setPosition();
		}
	}
	public void moveUp(int dist){
		if(posY<10&&this instanceof Jugador){
			
		}else{
			posY = posY-dist;
			setPosition();
		}		
	}
	public void moveDown(int dist){
		if(posY>(Pantalla.h-10-this.getOffsetHeight())&&this instanceof Jugador){
			
		}else{
			posY = posY+dist;
			setPosition();
		}		
	}
	public void setPosition(int x, int y){
		posX = x;
		posY = y;
		setPosition();
	}

	public void setPosition(){
		Pantalla.getPantalla().setWidgetPosition(this, posX, posY);
	}
	/**
	 * @return the posX
	 */
	public int getPosX() {
		return posX;
	}

	/**
	 * @return the posY
	 */
	public int getPosY() {
		return posY;
	}

	/**
	 * @param posX the posX to set
	 */
	public void setPosX(int posX) {
		this.posX = posX;
	}

	/**
	 * @param posY the posY to set
	 */
	public void setPosY(int posY) {
		this.posY = posY;
	}
	
}
